

package GUI;

import DataAccess.DBUtils;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class AccountCreate extends javax.swing.JFrame {
    /** Creates new form Login */
    private Container parent;
    public AccountCreate(Container parent) {
        initComponents();
        setLocationRelativeTo(null);   
        this.parent = parent;
        this.parent.setEnabled(false);
        setVisible(true);
        grTypeAccount.add(rdbtManager);
        grTypeAccount.add(rdbtUser);
        rdbtUser.setSelected(true);
        pnSalary.setVisible(false);
        windownClose();
    }
    public void windownClose(){
        addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {                
                 closeForm();               
            }
    });
    }
    public void closeForm(){
        parent.setEnabled(true);
        parent.requestFocus();
        dispose();  
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grTypeAccount = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        lbTitle = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        tfAccount = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfPass = new javax.swing.JPasswordField();
        btn_Exit1 = new javax.swing.JButton();
        btn_Login = new javax.swing.JButton();
        Pass_MatKhau2 = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        rdbtUser = new javax.swing.JRadioButton();
        rdbtManager = new javax.swing.JRadioButton();
        pnSalary = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        tfSalary = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tạo tài khoản");
        setBackground(new java.awt.Color(255, 255, 255));
        setName("Login"); // NOI18N
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(227, 239, 249));
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());
        jPanel1.add(jSeparator1, java.awt.BorderLayout.CENTER);

        lbTitle.setBackground(new java.awt.Color(255, 204, 204));
        lbTitle.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        lbTitle.setForeground(new java.awt.Color(0, 0, 204));
        lbTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbTitle.setText("Tạo tài khoản người sử dụng.");
        jPanel1.add(lbTitle, java.awt.BorderLayout.PAGE_START);

        jPanel3.setBackground(new java.awt.Color(227, 239, 249));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Tài Khoản", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 16), new java.awt.Color(102, 102, 0))); // NOI18N
        jPanel3.setForeground(new java.awt.Color(255, 255, 255));

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(102, 102, 0));
        jLabel5.setText("Tên Tài khoản : ");

        tfAccount.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        tfAccount.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        tfAccount.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfAccountKeyPressed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(102, 102, 0));
        jLabel6.setText("Mật khẩu : ");

        tfPass.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        tfPass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfPassKeyPressed(evt);
            }
        });

        btn_Exit1.setFont(new java.awt.Font("Times New Roman", 1, 13)); // NOI18N
        btn_Exit1.setForeground(new java.awt.Color(204, 0, 0));
        btn_Exit1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Exit_22.png"))); // NOI18N
        btn_Exit1.setText("Đóng");
        btn_Exit1.setToolTipText("");
        btn_Exit1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_Exit1ActionPerformed(evt);
            }
        });

        btn_Login.setFont(new java.awt.Font("Times New Roman", 1, 13)); // NOI18N
        btn_Login.setForeground(new java.awt.Color(0, 0, 255));
        btn_Login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Account.png"))); // NOI18N
        btn_Login.setText("Tạo mới");
        btn_Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_LoginActionPerformed(evt);
            }
        });

        Pass_MatKhau2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        Pass_MatKhau2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                Pass_MatKhau2KeyPressed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(102, 102, 0));
        jLabel7.setText("Xác nhận mật khẩu : ");

        rdbtUser.setBackground(new java.awt.Color(227, 239, 249));
        rdbtUser.setText("Tài khoản người dùng");
        rdbtUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbtUserActionPerformed(evt);
            }
        });

        rdbtManager.setBackground(new java.awt.Color(227, 239, 249));
        rdbtManager.setText("Tài khoản quản trị viên");
        rdbtManager.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbtManagerActionPerformed(evt);
            }
        });

        pnSalary.setBackground(new java.awt.Color(227, 239, 249));

        jLabel8.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(102, 102, 0));
        jLabel8.setText("Số điện thoại :");

        tfSalary.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        tfSalary.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        tfSalary.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfSalaryKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout pnSalaryLayout = new javax.swing.GroupLayout(pnSalary);
        pnSalary.setLayout(pnSalaryLayout);
        pnSalaryLayout.setHorizontalGroup(
            pnSalaryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnSalaryLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(tfSalary, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnSalaryLayout.setVerticalGroup(
            pnSalaryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnSalaryLayout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addGroup(pnSalaryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tfSalary, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(37, 37, 37)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfPass)
                            .addComponent(tfAccount)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Pass_MatKhau2))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rdbtUser)
                            .addComponent(btn_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rdbtManager)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addComponent(btn_Exit1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(29, 29, 29)))
                .addContainerGap())
            .addComponent(pnSalary, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tfAccount, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tfPass, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(Pass_MatKhau2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addComponent(pnSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rdbtUser)
                    .addComponent(rdbtManager))
                .addGap(28, 28, 28)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Exit1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Pass_MatKhau2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Pass_MatKhau2KeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_Pass_MatKhau2KeyPressed

    private void btn_LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_LoginActionPerformed
        accountTest();
    }//GEN-LAST:event_btn_LoginActionPerformed

    private void btn_Exit1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_Exit1ActionPerformed
        // TODO add your handling code here:
        closeForm();
    }//GEN-LAST:event_btn_Exit1ActionPerformed

    private void tfPassKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPassKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfPassKeyPressed

    private void tfAccountKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfAccountKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfAccountKeyPressed

    private void tfSalaryKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfSalaryKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfSalaryKeyPressed

    private void rdbtUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbtUserActionPerformed
        // TODO add your handling code here:
        pnSalary.setVisible(false);
        lbTitle.setText("Tạo tài khoản người sử dụng.");
    }//GEN-LAST:event_rdbtUserActionPerformed

    private void rdbtManagerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbtManagerActionPerformed
        // TODO add your handling code here:
        pnSalary.setVisible(true);
        lbTitle.setText("Tạo tài khoản quản lý.");
    }//GEN-LAST:event_rdbtManagerActionPerformed
    public void diseanabledChoseAccount(){
        rdbtManager.setEnabled(false);
        rdbtUser.setEnabled(false);
    }
    public void accountTest(){
        String account = tfAccount.getText();
        String pass = String.valueOf(tfPass.getPassword());
        String passConfirm = String.valueOf(Pass_MatKhau2.getPassword());
        int salary = 0;
        String table = "user";
        int id = 1;
        if (account.equals("")){
            JOptionPane.showMessageDialog(null,"Tài khoản rỗng.\nHãy nhập lại!","Tên tài khoản rỗng!",JOptionPane.ERROR_MESSAGE);
            tfAccount.requestFocus();
        } else if (pass.equals("")){
            JOptionPane.showMessageDialog(null,"Mật khẩu rỗng.\nHãy nhập lại!","Mật khẩu rỗng!",JOptionPane.ERROR_MESSAGE);
            tfPass.requestFocus();
        } 
        else if (passConfirm.equals("")){
            JOptionPane.showMessageDialog(null,"Mật khẩu xác nhận rỗng.\nHãy nhập lại!","Mật khẩu xác nhận rỗng!",JOptionPane.ERROR_MESSAGE);
            Pass_MatKhau2.requestFocus();
        } 
        else if (!passConfirm.equals(pass)){
            JOptionPane.showMessageDialog(null,"Mật khẩu không giống.\nHãy nhập lại!","Mật khẩu xác nhận sai!",JOptionPane.ERROR_MESSAGE);
            tfPass.requestFocus();            
            tfPass.setText("");            
            Pass_MatKhau2.setText("");
        }  
        else {
            if(rdbtManager.isSelected()) {
                table = "employee";
                id = 2;                
                try{
                    salary = Integer.parseInt(tfSalary.getText());
                }
                catch(Exception e){
                   JOptionPane.showMessageDialog(this,"Tiền lương phải là số nguyên.");
                   tfSalary.requestFocus();
                   return;
                }
            }
            try {
                String query = "Select * from " + table;
                ResultSet rs = Login.dtConnect.retriveData(query);
                boolean newAccount = true;
                while (rs.next()) {
                    if(rs.getString(id).equals(account))
                    {
                        newAccount = false;
                        break;                    
                    }
                }
                if(newAccount){
                    if(table.equals("employee")) query = "insert into employee (EMPLOYEENAME, PASSWORD, PHONE) value ('" + account + "','" + pass + "','" + salary + "')";       
                    else query = "insert into user (USERNAME, PASSWORD) value ('" + account + "', '" + pass + "')";       
                    JOptionPane.showMessageDialog(this,"Tài khoản tạo thành công!");
                    Login.dtConnect.updateData(query);
                    closeForm();
                }
                else{
                    JOptionPane.showMessageDialog(this,"Tài khoản đã tồn tại!");
                }
            } catch (SQLException ex) {
            }
        }
    }                
    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField Pass_MatKhau2;
    private javax.swing.JButton btn_Exit1;
    private javax.swing.JButton btn_Login;
    private javax.swing.ButtonGroup grTypeAccount;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lbTitle;
    private javax.swing.JPanel pnSalary;
    private javax.swing.JRadioButton rdbtManager;
    private javax.swing.JRadioButton rdbtUser;
    private javax.swing.JTextField tfAccount;
    private javax.swing.JPasswordField tfPass;
    private javax.swing.JTextField tfSalary;
    // End of variables declaration//GEN-END:variables
    
}
